********************************************************************************
** 	TITLE: incident_JMS_merged.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Sister do-file to merge_JMS, cleans the jail incident report data
**			 and merges JMS Booking and Incidents Data. 

********************************************************************************
		set sortseed 13

//Bring in the JMS Incidents Data
import delimited "$input_data/jms_all_22dec2022/jailincidentreport.csv", clear varnames(1)

gen incident_date = date(incidentdate,"MDY")
format incident_date %td
ren involvedinmate fullname
replace fullname = proper(fullname)
drop if mi(fullname)
split fullname, p(" ")
ren fullname1 last
ren fullname2 first
replace last=subinstr(last,",","",.)

gen ym_in = mofd(incident_date)
format ym_in %tm


preserve

use "$output_data/001_merge_JMS_$date.dta", clear
replace fullname = proper(fullname)
drop if mi(fullname)
split fullname, p(" ")
ren fullname1 last
ren fullname2 first
replace last=subinstr(last,",","",.)
tempfile jms_base
save `jms_base', replace

restore

joinby last first using `jms_base'


keep if (booking_date<=incident_date&incident_date<=release_date&!mi(release_date))|(booking_date<=incident_date&mi(release_date))


drop if mi(fullname)

keep  inmate booking_date release_date incident_date incidentforceused incidentlognumber incidenttype incidentdate1 location

bys inmate booking_date release_date: gen id = _n

ren incidentdate1 incidenttime

sreshape wide incident_date incidenttime incidentforceused incidentlognumber incidenttype location, ///
	i(inmate booking_date release_date) j(id)

gen first_maj_date=.
gen first_sui_date=.
gen D_incident=1
gen D_maj=0
gen num_incident = 0
gen num_maj = 0
gen D_sui=0
gen num_sui = 0
gen D_med=0
gen num_med = 0
gen D_min=0
gen num_min = 0
gen D_disobey = 0
gen D_mis_violece = 0
gen num_maj_not_other = 0
gen num_maj_not_ignite_hr = 0
gen num_maj_not_tablet_hr = 0

forvalues j=1/88{
	cap replace num_incident=num_incident+1 if !mi(incidentlognumber`j')
	cap replace D_maj = 1 if strpos(incidenttype`j',"MAJ")
	cap replace num_maj = num_maj+1 if strpos(incidenttype`j',"MAJ")
	cap replace D_min = 1 if strpos(incidenttype`j',"MIN")
	cap replace num_min = num_maj+1 if strpos(incidenttype`j',"MIN")
	cap replace D_sui = 1 if strpos(incidenttype`j',"SUI")
	cap replace num_sui  = num_sui+1 if strpos(incidenttype`j',"SUI")
	cap replace D_med = 1 if strpos(incidenttype`j',"MED")
	cap replace num_med = num_med+1 if strpos(incidenttype`j',"MED")
	cap replace first_maj_date = incident_date`j' if ///
		(first_maj_date>=incident_date`j'|mi(first_maj_date)) ///
		&strpos(incidenttype`j',"MAJ")
	
	cap replace first_sui_date = incident_date`j' if ///
		(first_sui_date>=incident_date`j'|mi(first_sui_date)) ///
		&strpos(incidenttype`j',"SUI")
		
	cap replace D_disobey = 1 if strpos(incidenttype`j',"30")
	cap replace D_mis_violece = 1 if strpos(incidenttype`j',"34")|strpos(incidenttype`j',"35")|strpos(incidenttype`j',"36")|strpos(incidenttype`j',"37")|strpos(incidenttype`j',"38") ///
			|strpos(incidenttype`j',"39")|strpos(incidenttype`j',"41")|strpos(incidenttype`j',"42")
			
	 replace num_maj_not_other = num_maj_not_other+1 if strpos(incidenttype`j',"MAJ")&( strpos(incidenttype`j',"22")|strpos(incidenttype`j',"23")| strpos(incidenttype`j',"24")| ///
		strpos(incidenttype`j',"25")|strpos(incidenttype`j',"27")| strpos(incidenttype`j',"28") | strpos(incidenttype`j',"29")|strpos(incidenttype`j',"30")| strpos(incidenttype`j',"32")| ///
		strpos(incidenttype`j',"33")|strpos(incidenttype`j',"37") )

//Non-IGNITE Hours; IGNITE hour varies by floor: Floor 3 8am & 11am, Floor 4 10am & 3pm, Floor 5 9am & 1pm		
	 replace num_maj_not_ignite_hr = num_maj_not_ignite_hr+1 if strpos(incidenttype`j',"MAJ")& ///
		!(strpos(location`j',"3")==1&strpos(incidenttime`j',"8:")&strpos(incidenttime`j',"AM")) & ///
		!(strpos(location`j',"3")==1&strpos(incidenttime`j',"11:")&strpos(incidenttime`j',"AM")) & ///
		!(strpos(location`j',"4")==1&strpos(incidenttime`j',"10:")&strpos(incidenttime`j',"AM")) & ///
		!(strpos(location`j',"4")==1&strpos(incidenttime`j',"3:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(location`j',"5")==1&strpos(incidenttime`j',"9:")&strpos(incidenttime`j',"AM")) & ///
		!(strpos(location`j',"5")==1&strpos(incidenttime`j',"1:")&strpos(incidenttime`j',"PM")) 

//Non-Tablet Hours: Hours outside of 5pm-10pm; and drop hour after the second IGNITE hour (last IGNITE hour varies by floor: Floor 3 11am-12pm, Floor 4 3pm-4pm, Floor 5 1pm-2pm)
	 replace num_maj_not_tablet_hr = num_maj_not_tablet_hr+1 if strpos(incidenttype`j',"MAJ")& /// Tablet Hours from 5pm-10pm
		!(strpos(incidenttime`j',"5:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(incidenttime`j',"6:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(incidenttime`j',"7:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(incidenttime`j',"8:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(incidenttime`j',"9:")&strpos(incidenttime`j',"PM")) & /// Tablet hours: hour after 2nd hour of IGNITE (varies by location)
		!(strpos(location`j',"3")==1&strpos(incidenttime`j',"12:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(location`j',"4")==1&strpos(incidenttime`j',"4:")&strpos(incidenttime`j',"PM")) & ///
		!(strpos(location`j',"5")==1&strpos(incidenttime`j',"2:")&strpos(incidenttime`j',"PM"))
	}
format first_maj_date %td
forma first_sui_date %td

//Saving the JMS Incident Reports data
save "$output_data/incident_JMS_merged_$date.dta", replace

//Bring in the JMS Cleaned booking data
use "$output_data/001_merge_JMS_$date.dta", clear

//Merge JMS Booking with JMS Incidents data 	
	merge 1:1 inmate booking_date release_date using "$output_data/incident_JMS_merged_$date.dta"
	
	gen time_to_maj = first_maj_date - booking_date
	
	gen time_to_sui = first_sui_date - booking_date
	
	drop _merge

	foreach n in incident maj sui med{
		replace D_`n'=0 if mi(D_`n')
		replace num_`n'=0 if mi(num_`n')
		gen davg_num_`n' = num_`n'/stay_misc
		gen wavg_num_`n' = davg_num_`n'*7
	}
	replace D_disobey=0 if mi(D_disobey) 
	replace D_mis_violece =0 if mi(D_mis_violece) 
	
	* Label variables
		la var inmate "Inmate number"
		la var fullname "Inmate name"
		la var booking_date "Booking start date"
		la var release_date "Inmate released date" 
		la var dob "Inmate date of birth" 
		la var race "Inmate race"
		la var sex "Inmate sex"
		la var D_Black "=1 if inmate race is Black" 
		la var D_White "=1 if inmate race is White"
		la var D_female "=1 if inmate sex is female"
		la var stay "Length of incarceration in days"
		
		order inmate fullname booking_date release_date stay_misc ///
			dob race sex D_Black D_White D_female

//Saving JMS Booking + Incidents Data		
save "$output_data/001_merge_JMS_incident_$date.dta", replace

